<template>
  <basic-container>
    <avue-crud :option="option"
               :data="data"
               :page="page"
               @row-del="rowDel"
               v-model="form"
               :permission="permissionList"
               @row-update="rowUpdate"
               @row-save="rowSave"
               :before-open="beforeOpen"
               @search-change="searchChange"
               @search-reset="searchReset"
               @selection-change="selectionChange"
               @on-load="onLoad">
      <template slot="menuLeft">
        <el-button type="danger"
                   size="small"
                   icon="el-icon-delete"
                   plain
                   v-if="permission.appArea_delete"
                   @click="handleDelete">删 除
        </el-button>
      </template>

      <template slot="menu"
                slot-scope="scope">
        <el-button
          size="small"
          type="text"
          v-if="permission.apparea_view"
          icon="el-icon-edit"
          @click="handlerMap(scope.row.id)">计算详情
        </el-button>
      </template>

    </avue-crud>

    <el-dialog title="提示"
               :visible.sync="map"
               height="900px"
               width="1200px">
      <mapIframe :src="src"></mapIframe>
      <span slot="footer"
            class="dialog-footer">
        <el-button @click="map = false">取 消</el-button>
        <el-button type="primary">确 定</el-button>
      </span>
    </el-dialog>


  </basic-container>
</template>

<script>
  import {getList, getDetail, add, update, remove} from "@/api/app/apparea";
  import {mapGetters} from "vuex";
  import mapIframe from '@/components/map/main';

  export default {
    components: {mapIframe},
    data() {
      return {
        //  iframeUrl: "http://107.182.16.8:81/web/cal",
        iframeUrl: "http://127.0.0.1:81/web/cal",
        src: "",
        map: false,
        form: {},
        page: {
          pageSize: 10,
          currentPage: 1,
          total: 0
        },
        selectionList: [],
        option: {
          tip: false,
          border: true,
          index: true,
          viewBtn: true,
          selection: true,
          column: [
            {
              label: "",
              prop: "id",
              hide: true,
              rules: [{
                required: true,
                message: "请输入",
                trigger: "blur"
              }]
            },
            {
              label: "无人机编号",
              prop: "no",
              search: true,
              rules: [{
                required: true,
                message: "请输入无人机编号",
                trigger: "blur"
              }]
            },
            {
              label: "操作用户",
              search: true,
              prop: "account",
              rules: [{
                required: true,
                message: "请输入",
                trigger: "blur"
              }]
            },
            {
              label: "用户昵称",
              prop: "userName",
              rules: [{
                required: true,
                message: "请输入",
                trigger: "blur"
              }]
            },
            {
              label: "计算时间",
              prop: "createDate",
              rules: [{
                required: true,
                message: "请输入",
                trigger: "blur"
              }]
            },
            {
              label: "坐标数量",
              prop: "count",
              rules: [{
                required: true,
                message: "请输入地面高度",
                trigger: "blur"
              }]
            },
            {
              label: "计算结果",
              prop: "result",
              rules: [{
                required: true,
                message: "请输入计算结果",
                trigger: "blur"
              }]
            },
          ]
        },
        data: []
      };
    },
    computed: {
      ...mapGetters(["permission"]),
      permissionList() {
        return {
          addBtn: false,
          viewBtn: false,
          delBtn: this.vaildData(this.permission.appArea_delete, false),
          editBtn: false
        };
      },
      ids() {
        let ids = [];
        this.selectionList.forEach(ele => {
          ids.push(ele.id);
        });
        return ids.join(",");
      }
    },
    methods: {
      handlerMap(id) {
        this.src = this.iframeUrl + "?id=" + id;
        this.map = true
      },
      rowSave(row, loading) {
        add(row).then(() => {
          loading();
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!"
          });
        });
      },
      rowUpdate(row, index, loading) {
        update(row).then(() => {
          loading();
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!"
          });
        });
      },
      rowDel(row) {
        this.$confirm("确定将选择数据删除?", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        })
          .then(() => {
            return remove(row.id);
          })
          .then(() => {
            this.onLoad(this.page);
            this.$message({
              type: "success",
              message: "操作成功!"
            });
          });
      },
      searchReset() {
        this.onLoad(this.page);
      },
      searchChange(params) {
        this.onLoad(this.page, params);
      },
      selectionChange(list) {
        this.selectionList = list;
      },
      handleDelete() {
        if (this.selectionList.length === 0) {
          this.$message.warning("请选择至少一条数据");
          return;
        }
        this.$confirm("确定将选择数据删除?", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        })
          .then(() => {
            return remove(this.ids);
          })
          .then(() => {
            this.onLoad(this.page);
            this.$message({
              type: "success",
              message: "操作成功!"
            });
            this.$refs.crud.toggleSelection();
          });
      },

      beforeOpen(done, type) {
        if (["edit", "view"].includes(type)) {
          getDetail(this.form.id).then(res => {
            this.form = res.data.data;
          });
        }
        done();
      },
      onLoad(page, params = {}) {
        getList(page.currentPage, page.pageSize, params).then(res => {
          const data = res.data.data;
          this.page.total = data.total;
          this.data = data.records;
        });
      }
    }
  };
</script>

<style>
</style>
